package com.example.examsystem.repository;

import com.example.examsystem.model.ExamAnswer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 考试答案数据访问层
 */
@Repository
public interface ExamAnswerRepository extends JpaRepository<ExamAnswer, Long> {
    
    /**
     * 根据考试记录ID查找答案
     */
    List<ExamAnswer> findByExamRecordId(Long examRecordId);
    
    /**
     * 根据考试记录ID和题目ID查找答案
     */
    Optional<ExamAnswer> findByExamRecordIdAndQuestionId(Long examRecordId, Long questionId);
    
    /**
     * 统计考试记录的答案数量
     */
    long countByExamRecordId(Long examRecordId);
    
    /**
     * 删除考试记录的所有答案
     */
    @Modifying
    @Query("DELETE FROM ExamAnswer ea WHERE ea.examRecordId = :examRecordId")
    void deleteByExamRecordId(@Param("examRecordId") Long examRecordId);
    
    /**
     * 检查答案是否存在
     */
    boolean existsByExamRecordIdAndQuestionId(Long examRecordId, Long questionId);
}
